package pathways;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import pathways.Database.Implementation.CDatabase;
import pathways.Database.Specialized.Visual.CSearchCompound;
import pathways.util.CUtil;
import pathways.util.Debug.CDebug;
import pathways.util.helper.CItem;
import pathways.util.helper.CItemComboModel;

public class CSearchPathway extends JDialog
{
  JButton cmdOK = new JButton();
  JButton cmdCancel = new JButton();
  JTextField txtPathwayName = new JTextField();
  JComboBox cboSearchBy = new JComboBox();
  JScrollPane scrollPane = new JScrollPane();
  JList lstPathways = new JList();
  CItemComboModel mdlcboSearchBy = null;
  CDatabase db = null;
  JFrame owner = null;
  CMyRSListModel mdllstPathways = null;
  CItem mSelectedItem = null;
  
  public CSearchPathway(CDatabase database, JFrame parent)
  {
    super(parent,true);
    
    initialize();
    
    db = database;   
    owner = parent;
  }
  
  public CSearchPathway()
  {
    initialize();
  }
  
  private void initialize()
  {
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    
    Vector v = new Vector();
    
    v.add(new CItem(1,"Substrate"));
    v.add(new CItem(2,"Pathway"));
    
    this.mdlcboSearchBy = new CItemComboModel(v);
    this.cboSearchBy.setModel(this.mdlcboSearchBy);
    
    setSize(350,340);    
  }
  
  private void jbInit() throws Exception
  {
    cmdOK.setText("OK");
    cmdOK.setBounds(new Rectangle(120, 279, 69, 25));
    cmdOK.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdOK_actionPerformed(e);
      }
    });
    this.getContentPane().setLayout(null);
    cmdCancel.setText("Cancel");
    cmdCancel.setBounds(new Rectangle(199, 279, 89, 25));
    cmdCancel.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdCancel_actionPerformed(e);
      }
    });
    txtPathwayName.setBounds(new Rectangle(120, 15, 212, 24));
    txtPathwayName.addKeyListener(new java.awt.event.KeyAdapter()
    {
      public void keyReleased(KeyEvent e)
      {
        txtPathwayName_keyReleased(e);
      }
    });
    cboSearchBy.setBounds(new Rectangle(11, 16, 102, 22));
    scrollPane.setBounds(new Rectangle(121, 60, 213, 208));
    this.setResizable(false);
    this.getContentPane().add(txtPathwayName, null);
    this.getContentPane().add(cboSearchBy, null);
    this.getContentPane().add(scrollPane, null);
    this.getContentPane().add(cmdOK, null);
    this.getContentPane().add(cmdCancel, null);
    scrollPane.getViewport().add(lstPathways, null);
  }

  void txtPathwayName_keyReleased(KeyEvent e)
  {
    if (e.getKeyCode() == KeyEvent.VK_ENTER)
    {
      // if substrate then show compound selection
      // and then search
      CItem item = (CItem)this.cboSearchBy.getSelectedItem();
      ResultSet rsPathways = null;
      
      // Substrate
      if (item.getID() == 1)
      {
        String strCmpdName = this.txtPathwayName.getText();
        
        CCompound cmpd = searchCompound(strCmpdName);
        
        if (cmpd == null)
           return;
        
        rsPathways = db.getSelect().getPathwaysBySubstrateID((int)cmpd.getID());       
      }
      else  // searching by PathwayName
      {
        String strPathwayName = this.txtPathwayName.getText();
        
        rsPathways = db.getSelect().getPathwaysByLikeNames(strPathwayName);
      }
      
      try{
        if (!rsPathways.next())
        {
          JOptionPane.showMessageDialog(this,"No Pathway was found.");
        }
      } catch(SQLException sqle) {}
            
      this.mdllstPathways = new CMyRSListModel(rsPathways);
      this.lstPathways.setModel(this.mdllstPathways);
    }
  }
  
  private CCompound searchCompound(String cmpdName)
  {
    CSearchCompound c = new CSearchCompound(true,owner,db,CSearchCompound.FILTER_NONE);
    c.searchFor(cmpdName);
    
    c.show();
    
    return c.getSelectedCompound();
  }

  void cmdOK_actionPerformed(ActionEvent e)
  {
    if (isOK())
    {
      mSelectedItem = this.mdllstPathways.getItem(this.lstPathways.getSelectedIndex());
      this.dispose();
    }
  }
  
  public CItem getSelectedPathway()
  {
    return mSelectedItem;  
  }
  
  private boolean isOK()
  {
    if (this.lstPathways.getSelectedIndex() < 0)
    {
      JOptionPane.showMessageDialog(this,"Please select a Pathway.","Warning",JOptionPane.WARNING_MESSAGE);
      return false; 
    }
    
    return true;
  }

  void cmdCancel_actionPerformed(ActionEvent e)
  {
    mSelectedItem = null;
    this.dispose();
  }
}

class CMyRSListModel extends AbstractListModel
{  
  public CMyRSListModel(ResultSet r)
  {
    data = r; 
  }
  
  public void setData(ResultSet r)
  {
    data = r;  
  }
  
  public ResultSet getData()
  {
    return data;  
  }
  
  public Object getElementAt(int index)
  {
    try {
        data.absolute(index+1);
        return data.getString(2);
    } catch(SQLException e) {
      CDebug.println(this, "getElementAt() - Access to element # " + (index+1) + " failed. Exception: " + e.toString());
      return null;
    }
  }
  
  public int getSize()
  { 
    try {     
        return CUtil.getResultSetRowCount(data);
    } catch(SQLException e) {
      CDebug.println(this, "getSize(). Exception: " + e.toString());
      return 0;
    }
  }
  
  public CItem getItem(int index)
  {
    try{
      data.absolute(index + 1);
      return (new CItem(data.getInt(1),data.getString(2)));
    } catch(SQLException e) {
      CDebug.println(this, "getCompound(). Exception: " + e.toString());
      return null;
    }
  }
  
  private ResultSet data;
}